package com.rem.photoablum.controller;

import com.rem.photoablum.model.Album;
import com.rem.photoablum.model.AlbumWithCount;
import com.rem.photoablum.model.User;
import com.rem.photoablum.model.UserWithCount;
import com.rem.photoablum.service.AlbumService;
import com.rem.photoablum.service.PhotoService;
import com.rem.photoablum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author remembrance
 * @version 1.0
 * @date 2022/5/19 9:36
 */
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private PhotoService photoService;
    @Resource
    private AlbumService albumService;

    //测试接口
    @GetMapping("/testAlbum")
    public AlbumWithCount testAlbum(){
        Album tempAlbum = new Album();
        tempAlbum.setAlbumDesc("123");
        tempAlbum.setAlbumName("123");
        tempAlbum.setAid(1L);
        tempAlbum.setPid(2);
        tempAlbum.setTopic("测试");
        tempAlbum.setPath("123123");
        AlbumWithCount album = new AlbumWithCount();
        album.setAlbum(tempAlbum);
        album.setPhotoCount(123);
        return album;
    }

    //测试接口
    @GetMapping("/testUser")
    public UserWithCount testUser(){
        User user = new User();
        user.setUsername("test");
        user.setId(123);
        user.setPassword("test");
        UserWithCount userWithCount = new UserWithCount();
        userWithCount.setUser(user);
        userWithCount.setAlbumCount(111);
        userWithCount.setPhotoCount(222);
        return userWithCount;
    }

    /****************************用户管理接口*******************************/
    /**
     * 添加用户 传入用户对象
     * @param user
     * @return
     */
    @PutMapping("/user")
    public String register(User user){
        User byUsername = userService.findByUsername(user.getUsername());
        if ( byUsername == null ){
            userService.addUser(user);
            return "ok";
        }else {
            return "error";
        }
    }

    /**
     * 根据用户名查询，返回用户对象，传入用户名
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public UserWithCount getUserWithCountByUsername(@PathVariable("username") String username){
        User user = userService.findByUsername(username);
        Integer photoCount = photoService.findAllByPid(user.getId()).size();
        Integer albumCount = albumService.findAlbumByPid(user.getId()).size();
        UserWithCount userWithCount = new UserWithCount();
        userWithCount.setUser(user);
        userWithCount.setPhotoCount(photoCount);
        userWithCount.setAlbumCount(albumCount);
        return userWithCount;
    }

    /**
     * 获取所有的用户对象，返回带数量的对象集合
     * @return
     */
    @GetMapping("/user")
    public List<UserWithCount> getAllUserWithCount(){
        List<User> userList = userService.findAll();
        List<UserWithCount> userWithCountList = new LinkedList<>();
        for (User user : userList) {
            Integer photoCount = photoService.findAllByPid(user.getId()).size();
            Integer albumCount = albumService.findAlbumByPid(user.getId()).size();
            UserWithCount userWithCount = new UserWithCount();
            userWithCount.setUser(user);
            userWithCount.setPhotoCount(photoCount);
            userWithCount.setAlbumCount(albumCount);
            userWithCountList.add(userWithCount);
        }
        return userWithCountList;
    }

    /**
     *  修改用户接口，需传入用户的id，username，password
     * @param user
     * @return
     */
    @PostMapping("/user")
    public String updateUser(User user){
        int i = userService.updateUser(user);
        if (i >0){
            return "ok";
        }
        return "error";
    }

    /**
     * 删除用户接口，传入用户的id
     * @param id
     * @return
     */
    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        int i = userService.deleteUserById(id);
        if (i>0){
            return "ok";
        }
        return "error";
    }

    /****************************相册管理接口*******************************/
    /**
     * 通过相册名查找相册，返回单个相册对象，传入相册名
     * @param albumName
     * @return
     */
    @GetMapping("/album/{albumName}")
    public AlbumWithCount getAlbumWithCountByAlbumName(@PathVariable("albumName") String albumName){
        Album album = albumService.findAlbumsByAlbumName(albumName);
        Integer albumCount = photoService.findAllByAid(album.getAid()).size();
        AlbumWithCount albumWithCount = new AlbumWithCount();
        albumWithCount.setAlbum(album);
        albumWithCount.setPhotoCount(albumCount);
        return albumWithCount;
    }

    /**
     * 获取所有相册对象，返回带有数量的相册对象的集合
     * @return
     */
    @GetMapping("/album")
    public List<AlbumWithCount> getAllAlbumWithCount(){
        List<Album> albumList = albumService.findAll();
        List<AlbumWithCount> albumWithCountList = new LinkedList<>();
        for (Album album : albumList) {
            Integer albumCount = photoService.findAllByAid(album.getAid()).size();
            AlbumWithCount albumWithCount = new AlbumWithCount();
            albumWithCount.setAlbum(album);
            albumWithCount.setPhotoCount(albumCount);
            albumWithCountList.add(albumWithCount);
        }
        return albumWithCountList;
    }

    /**
     * 修改相册接口，传入相册的aid,albumName,albumDesc,topic
     * @param album
     * @return
     */
    @PostMapping("/album")
    public String updateAlbumByAid(Album album){
        Integer i = albumService.updateByAid(album);
        if (i>0){
            return "ok";
        }
        return "error";
    }

    /***************************修改密码接口****************************/
    /**
     * 修改密码接口，需传入用户的id,password
     * @param user
     * @return
     */
    @PostMapping("/admin")
    public String updatePassword(User user){
        int i = userService.updateUser(user);
        if (i>0){
            return "ok";
        }
        return "error";
    }




}
